package com.cuckoo.cuckoo_app2.utils;

import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.media.AudioAttributes;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;

import com.cuckoo.cuckoo_app2.R;
import com.cuckoo.cuckoo_app2.enums.NotificationChannelsEnum;
import com.cuckoo.cuckoo_app2.ui.MainActivity;

public class NotificationUtils {

    private static final String CHANNEL_ID = "my_notification_channel";
    private static final String CHANNEL_NAME = "布谷鸟通知";
    private static final String CHANNEL_DESCRIPTION = "开启此服务以接收通知提醒";

    private final Context context;


    public NotificationUtils(Context context) {
        this.context = context;
        createNotificationChannel();
    }

    private void createNotificationChannel() {
        // 对于Android 8.0及以上版本，创建通知渠道
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
            channel.setDescription(CHANNEL_DESCRIPTION);
            // 允许声音和振动
            channel.enableLights(true);
            channel.setLightColor(Color.RED);
            channel.enableVibration(true);

            // 设置自定义声音
            Uri soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/" + R.raw.xiaoxi);
            AudioAttributes audioAttributes = new AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION).setUsage(AudioAttributes.USAGE_NOTIFICATION).build();
            channel.setSound(soundUri, audioAttributes);

            // 设置自定义震动模式
            long[] vibrationPattern = {0, 500, 500, 500}; // 自定义震动模式
            channel.setVibrationPattern(vibrationPattern);

            // 获取NotificationManager并创建通知渠道
            NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
    }

    public void sendNotification(String title, String message) {
        //设置服务跳转
        Intent intent = new Intent(context, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_MUTABLE);

        Notification.Builder builder = new Notification.Builder(context, CHANNEL_ID).setSmallIcon(R.drawable.logo_no).setContentTitle(title).setContentText(message).setContentIntent(pendingIntent).setAutoCancel(true).setPriority(Notification.PRIORITY_HIGH);

        // 发送通知
        NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
        notificationManager.notify(NotificationChannelsEnum.NOTIFICATION_CHANNEL_ID_MESSAGE, builder.build());
    }


    /**
     * 检测通知权限状态
     */
    public void checkAndPromptNotificationPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            if (!PermissionChecker.hasNotificationPermission(context)) {
                showNotificationDisabledDialog(context);
            }
        }
    }

    /**
     * 引导用户开启通知权限
     *
     * @param context
     */
    private void showNotificationDisabledDialog(Context context) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("通知被禁用").setMessage("请允许应用的通知权限以接收重要更新和通知。").setPositiveButton("去设置", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // 引导用户到系统设置页面
                Intent intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
                intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName());
                intent.putExtra(Settings.EXTRA_CHANNEL_ID, CHANNEL_ID);
                context.startActivity(intent);
            }
        }).setNegativeButton("取消", null).show();
    }

}

